/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scd.commons.result.Result;
import com.lap.scp.admin.ao.TemplateAO;
import com.lap.scp.admin.domain.TemplateDO;
import com.lap.scp.admin.query.TemplateQueryDO;

/**
 * <pre>
 * // TODO 注释
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Controller
@RequestMapping("template")
public class TemplateController {

	@Resource
	private TemplateAO templateAO;

	@RequestMapping(value = "delete/{templateId}", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView deleteById(@PathVariable int templateId) {
		ModelAndView andView = new ModelAndView("redirect:/template/list");
		Result<Integer> resp = templateAO.deleteById(templateId);
		if (!resp.isSuccess()) {
			//
		}
		return andView;
	}

	@RequestMapping(value = "insert", method = RequestMethod.POST)
	public ModelAndView insert(TemplateDO templateDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();
		Result<TemplateDO> resp = templateAO.insertTemplate(templateDO, error);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/template/list");
		} else {
			andView.setViewName("admin/templateAdd");
			andView.addObject("templateDO", templateDO);
		}
		return andView;
	}

	@RequestMapping(value = "get/{templateId}", method = RequestMethod.GET)
	public ModelAndView queryById(@PathVariable int templateId) {
		ModelAndView andView = new ModelAndView();
		Resp<TemplateDO> resp = templateAO.queryById(templateId);
		if (resp.isSuccess()) {
			andView.setViewName("admin/templateDetail");
			andView.addObject("templateDO", resp.getData());
		}
		return andView;
	}

	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryList(Page page, TemplateQueryDO templateDO) {
		ModelAndView andView = new ModelAndView("admin/templateList");
		PageResult<TemplateDO> pr = templateAO.queryPage(page, templateDO);
		andView.addObject("pr", pr);
		andView.addObject("templateDO", templateDO);
		return andView;
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public ModelAndView update(TemplateDO templateDO, BindingResult error) {
		ModelAndView andView = new ModelAndView();
		Result<Integer> result = templateAO.updateTemplate(templateDO, error);
		if (result.isSuccess()) {
			andView.setViewName("redirect:/template/list");
		} else {
			andView.setViewName("admin/templateUpdate");
			andView.addObject("templateDO", templateDO);
		}
		return andView;
	}

}
